#!/bin/bash
# Betsu Helper v0.2 beta
# Written by Alexa Hattori for Betsu project.

# Menu

not_ready_yet () {
    clear
    echo "Is not ready yet, be patient!"
}

main_menu () {
    select main in "User Management" "Transfering Files via scp" "Misc" "Betsu Specific"; do
	case $main in
	    "User Management" ) user_management; break;;
            "Transfering Files via scp" ) transfering_scp; break;;
	    "Misc" ) misc; break;;
	    "Betsu Specific" ) betsu_specific; break;;
	esac
    done					     
}

user_management () {
    select usermanage in "Add User" "Remove User" "Lock/Unlock User" "Give Root Access" "Main Menu"; do
	case $usermanage in
	    "Add User" ) add_user; break;;
	    "Remove User" ) remove_user; break;;
	    "Lock/Unlock User" ) lock_user; break;;
	    "Give Root Access" ) add_sudo; break;;
	    "Main Menu" ) clear; main_menu; break;;
	esac
    done
}

# User Management

add_user () {
    read -p "Enter username: " user_name
    read -p "Shell for user: " user_shell
    echo "Give access to sudo? (wheel group)"
    select wheel in "Yes" "No" "Cancel"; do
	case $wheel in
	    Yes ) useradd -m -G users,wheel,audio,video -s $user_shell $user_name; break;;
	    No ) useradd -m -G users,audio,video -s $user_shell $user_name; break;;
	    Cancel ) clear; main_menu; break;;
	esac
    done
    
}

remove_user () {
    read -p "Enter username: " user_name
    select removeuser in "Delete user account" "Delete user and home directory" "Cancel"; do
			 case $removeuser in
			     "Delete user account" ) killall -u username; userdel $user_name; break;;
			     "Delete user and home directory" ) kilall -u username; userdel -r $user_name; break;;
			     "Cancel" ) clear; main_menu; break;;
			 esac
		      done
			 
}

lock_user () {
    read -p "Enter username: " user_name
    select lockuser in "Lock User" "Unlock User" "Cancel"; do
	case $lockuser in
	    "Lock User" ) usermod -L $user_name; break;;
	    "Unlock User" ) passwd $user_name; usermod -U $user_name; break;;
	    "Cancel" ) clear; main_menu; break;;
	esac
    done
    
}

add_sudo () {
    read -p "Enter username: " user_name
    select sudo in "Give Sudo Access" "Remove Sudo Access" "Cancel"; do
	case $sudo in
	    "Give Sudo Access" ) usermod -a -G wheel $user_name; break;;
	    "Remove Sudo Access" ) gpasswd -d $user_name wheel; break;;
	    "Cancel" ) clear; main_menu; break;;
	esac
    done
    
}

# Misc

misc () {
    select miscm in "Clean Logs" "Main Menu"; do
	case $miscm in
	    "Clean Logs" ) clean_logs; break;;
	    "Main Menu" ) clear; main_menu; break;;
	esac
    done
}

clean_logs () {
    echo "[ INFO ] Cleaning all files in /var/log/"
    find /var/log/ -type f -exec sh -c '>"{}"' \;
}

# scp transfering

transfering_scp () {
    select transferscp in "Files to Server" "Directory to Server" "Files from Server" "Directory from Server" "Main Menu"; do
	case $transferscp in
	    "Files to Server" ) file_to_server; break;;
	    "Directory to Server" ) directory_to_server; break;;
	    "Files from Server" ) file_from_server; break;;
	    "Directory from Server" ) directory_from_server; break;;
	    "Main Menu" ) clear; main_menu; exit;;
	esac
    done	   
}

file_to_server () {
    read -p "Enter username: " user_name
    read -p "Enter hostname: " hostname
    read -p "Files to transfer: " files
    read -p "Destination directory: " dest_dir

    echo "[ DEBUG ] Betsu-Helper | scp" $files $user_name"@"$hostname":"$dest_dir

    scp $files $user_name@$hostname:$dest_dir
}

directory_to_server () {
    read -p "Enter username: " user_name
    read -p "Enter hostname: " hostname
    read -p "Directory to transfer: " directory
    read -p "Destination directory: " dest_dir

    echo "[ DEBUG ] Betsu-Helper | scp -r" $directory $username"@"$hostname":"$dest_dir
    
    scp -r $directory $username@$hostname:$dest_dir
}

files_from_server () {
    echo "[ DEBUG ] file_from_server"
}

directory_from_server () {
    echo "[ DEBUG ] directory_from_server"
}

# Betsu Specific

betsu_specific () {
    not_ready_yet
}

echo "Betsu Helper is currently in early-beta version, it is not recommended to use it."
echo "Continue at your own risk. Do you want to continue?"
select yn in "Yes" "No"; do
    case $yn in
	"Yes" ) clear; main_menu; break;;
	"No" ) exit;
    esac
done
